package tms.presentation.bean.time;

import tms.business.time.TimeService;
import tms.model.time.TimeEntity;
import tms.util.localization.LocalizationManager;
import tms.util.security.AppSecurityManager;

import javax.ejb.EJB;
import javax.enterprise.context.SessionScoped;
import javax.inject.Inject;
import javax.inject.Named;
import java.io.Serializable;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Named
@SessionScoped
public class SessionScopedBean implements Serializable {
    @EJB
    private TimeService timeService;
    @Inject
    LocalizationManager localizationManager;
    @Inject
    AppSecurityManager securityManager;

    private Date currentDate = new Date();

    private Map<Integer, Boolean> timeStates;


    public Map<Integer, Boolean> getTimeStates() {
        if (timeStates == null) {
            timeStates = new HashMap<Integer, Boolean>();
            List<TimeEntity> currentWeek = timeService.getCurrentWeekTimes(currentDate, securityManager.getUser().getId(), localizationManager.getLocale());
            for (TimeEntity timeEntity : currentWeek) {
                timeStates.put(timeEntity.getId(), Boolean.FALSE);
            }
        }
        return timeStates;
    }

    public void resetTimeStates() {
        timeStates = null;
    }

    public Date getCurrentDate() {
        return currentDate;
    }

    public void setCurrentDate(Date currentDate) {
        this.currentDate = currentDate;
    }
}
